package cool.taomu.toolkit.micrometer.provider

import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics
import io.micrometer.core.instrument.binder.jvm.JvmCompilationMetrics
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics
import io.micrometer.core.instrument.binder.jvm.JvmHeapPressureMetrics
import io.micrometer.core.instrument.binder.jvm.JvmInfoMetrics
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics
import io.micrometer.core.instrument.binder.jvm.JvmThreadDeadlockMetrics
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics
import io.micrometer.core.instrument.binder.system.ProcessorMetrics
import io.micrometer.prometheusmetrics.PrometheusConfig
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
import javax.inject.Provider

class MicrometerProvider implements Provider<MeterRegistry> {

	def void registerJvmMetrics(MeterRegistry meterRegistry) {
		// 内存监控
		new JvmMemoryMetrics().bindTo(meterRegistry);
		// 线程监控
		new JvmThreadMetrics().bindTo(meterRegistry);
		// GC 活动监控
		new JvmGcMetrics().bindTo(meterRegistry);
		// Classloader 使用率监控
		new ClassLoaderMetrics().bindTo(meterRegistry);
		// Classloader 使用率监控
		new JvmHeapPressureMetrics().bindTo(meterRegistry);
		new JvmThreadDeadlockMetrics().bindTo(meterRegistry);
		new JvmInfoMetrics().bindTo(meterRegistry);
		new JvmCompilationMetrics().bindTo(meterRegistry);
		// CPU 使用率监控
		new ProcessorMetrics().bindTo(meterRegistry);
	}

	override get() {
		var meterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
		registerJvmMetrics(meterRegistry);
		return meterRegistry
	}

}
